#!/usr/bin/env python
#
# Copyright 2014 Ville Rantanen
#
# this program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import Qalbum
from argparse import ArgumentParser 
import os

# (c) ville.rantanen@helsinki.fi


def traverse(path,options):
    ''' The recursive main function to create the thumbs and seek sub folders '''
    print(path)
    pathlist=Qalbum.getpathlist(path,options)
    imagelist=Qalbum.getimagelist(path,options)
    print(str(len(pathlist))+' paths')
    print(str(len(imagelist))+' images')
    if options.clean:
        Qalbum.cleanthumbs(path)
    Qalbum.createthumbs(path,imagelist,options)
    if options.recursive:
        for p in pathlist:
            traverse(os.path.join(path,p),options)
    return

def setupoptions():
    ''' Setup options '''
    usage='''Usage: %(prog)s [options] folder
folder is the root folder of the image album (defaults to current folder).'''
    parser=ArgumentParser(description=usage)
    parser.add_argument("-v",action='version', version=Qalbum.__version__)
    parser.add_argument("-f",action="store_true",dest="force",default=False,
                      help="Force regeneration of thumbnails and medium sized")
    parser.add_argument("-c",action="store_true",dest="clean",default=False,
                      help="Clean unused thumbnails")
    parser.add_argument("-l",action="store_true",dest="link",default=False,
                      help="Create symbolic link as medium size image instead of downscaling one.")
    parser.add_argument("-r",action="store_true",dest="recursive",default=False,
                      help="Recurse in to subfolders")
    parser.add_argument("-w",type=int,dest="width",default=850,
                      help="Medium image size (Default: %(default)s)")
    parser.add_argument("--gravity",type=str,dest="gravity",default="Center",
                      help="ImageMagick gravity for cropping. (Default: %(default)s)")
    parser.add_argument("startpath",type=str,action="store",default=os.path.abspath('.'),nargs='?',
                      help="Start path for recursion")
    options=parser.parse_args()
    options.startpath=os.path.abspath(options.startpath)
    options=Qalbum.setupdefaultoptions(options)
    return options

def execute():
    ''' Main execution '''
    options=setupoptions()
    traverse(options.startpath,options)
    return

if __name__ == "__main__":
    execute()
   

